package com.freshdirect.src.controller;

import com.freshdirect.src.model.Order;
import com.freshdirect.src.model.ShoppingCart;
import com.freshdirect.src.model.User;
import com.freshdirect.src.model.View;
import com.freshdirect.src.service.OrderService;
import com.freshdirect.src.service.ShoppingCartService;
import com.freshdirect.src.service.UserService;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserController {
	
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String home(HttpServletRequest request, HttpServletResponse response,Model model,HttpSession session) throws IOException {
		if(session.getAttribute("userName")!=null){
               return "home";
            }
            else {
               response.sendRedirect(request.getContextPath());
               return null;}
	}


        @RequestMapping(value="/user/mycart", method = RequestMethod.GET)
         public String myCart(Model model,HttpSession session) {
                    if(session.getAttribute("cart")!=null){
                        ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                        ShoppingCartService scs= new ShoppingCartService();
                        cart = scs.checkAvailability(cart);
                        session.setAttribute("cart", cart);
                        model.addAttribute("cart",session.getAttribute("cart"));
                    }
                    return "mycart";
            }
        @RequestMapping(value="/user/faq", method = RequestMethod.GET)
         public String faq(HttpServletRequest request, HttpServletResponse response,Model model,HttpSession session) throws IOException  {
                if(session.getAttribute("userName")!=null){
               View view =new View();
               model.addAttribute("view", view);
               return "faq";
            }
            else {
               response.sendRedirect(request.getContextPath());
               return null;}
        }
         
         @RequestMapping(value="user/faq", method = RequestMethod.POST)
         public String test2(HttpServletRequest request, HttpServletResponse response,View view,HttpSession session,Model model) throws IOException  {
                if(session.getAttribute("userName")!=null){
                    model.addAttribute("m", view.getm());
                    model.addAttribute("y", view.gety());
                    

               return "faq";
            }
            else {
               response.sendRedirect(request.getContextPath());
               return null;}
        }
         
         
         
         @RequestMapping(value="/user/myaccount", method = RequestMethod.GET)
        public String myaccount(HttpServletRequest request, HttpServletResponse response,Model model,HttpSession session) throws IOException {
            if(session.getAttribute("userName")!=null){
               UserService userService= new UserService();
       User user = userService.ViewUserdetais((String) session.getAttribute("userName"));
        model.addAttribute("user",user);
     
               return "myaccount";
            }
            else {
               response.sendRedirect(request.getContextPath());
               return null;}
        }
        
        @RequestMapping(value="/user/myaccount",method = RequestMethod.POST)
    public String processUpdate( @Valid @ModelAttribute("user") User user,
            BindingResult result,Model model,HttpSession session)  {
      if (result.hasErrors()) {
          return "myaccount";
        }
      else{
        user.setId((Integer) session.getAttribute("userID"));
        UserService userService= new UserService();
        userService.updateUser(user);
        String result_s =  user.getResult();
        if(result_s.equals("Please Change Username")){
            session.setAttribute("update",6);
        model.addAttribute("result",result_s);
            return "myaccount";
        }
        else{

       model.addAttribute("userName", user.getUserName());
        model.addAttribute("result", user.getResult());
        session.setAttribute("update",2);
     
        return "myaccount";}
        }
    }
        
         @RequestMapping(value="/user/myorders", method = RequestMethod.GET)
         public String myorders(HttpServletRequest request,Model model,HttpSession session) throws IOException{           
             if(session.getAttribute("userName")!=null){
               OrderService os = new OrderService();
               List<Order> orders = os.getAllOrders((Integer) session.getAttribute("userID"));
               model.addAttribute("orders", orders);
               return "myorders";
            }
            else {
               return "redirect:/";
             }
        }
         
         @RequestMapping(value="/user/{id}/orders", method = RequestMethod.GET)
         public String vieworders(@PathVariable int id,Model model,HttpSession session) throws IOException{   
            
             if(session.getAttribute("adminName")!=null){
                 OrderService us = new OrderService();
                 List<Order> orders = us.getAllOrders(id);

                 model.addAttribute("orders", orders);

                 return "vieworders";
             }
             else{
                 return "redirect:/";
             }
        }
        
}
